package com.ilock.util;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ilock.bean.Iuser;

public class JsonTransfer {
	
	@Test
	public void maptoJson(){
		Gson gson = new Gson();
		Map<String,Object> maps = new HashMap<String, Object>();
		maps.put("json", "json");
		maps.put("object", 1);
		maps.put("dail", "dail");
		String json = gson.toJson(maps);
		System.out.println(json);
		List<Map<String,Object>> lists = new ArrayList<Map<String, Object>>();
		lists.add(maps);
		lists.add(maps);
		lists.add(maps);
		JsonObject  jo =new JsonObject();
		JsonArray ja = new JsonArray();	
		
		Map<String,Object> maps1 = new HashMap<String, Object>();
		maps1.put("email", "37398453@qq.com");
		maps1.put("password", "3945873294");
		maps1.put("lockid", 574298121);
		maps1.put("lockpassword", "sdafasg3453121");
		String json1 = gson.toJson(maps1);
		//System.out.println(json1);
		
		Map<String,Object> maps2 = new HashMap<String,Object>();
		maps2=gson.fromJson(json1, new TypeToken<Map<String, Object>>() {}.getType());
		for(Map.Entry<String,Object> map:maps2.entrySet()){
			System.out.println(map.getKey()+" "+map.getValue());	
			}
		
		//different type include list in json
		Map<String,Object> maps3 = new HashMap<String,Object>();
		maps3.put("list", lists);
		maps3.put("moneytype","dollar");
		String json2 = gson.toJson(maps3);
		System.out.println(maps3);
	}
	
	/**
	 * change a java bean to json type
	 * @param object
	 * @return
	 */
	public String beantoJson(Object object){
		Gson gson = new Gson();
		String json = gson.toJson(object);
		return json;
	}

}
